/*
Run this file to estimate the SVAR.
User only needs to specify the local path of this file.
*/

clear all
clear matrix
set more off
set maxvar 30000
set matsize 1000
set more off

*************************************************************
* Set path here
*************************************************************
	*cd "/Users/matiasmoretti/Dropbox/Partial_Default/Replication_Package/Empirics"
	 cd "/Users/jmorelli/Dropbox/Research/Work_in_progress/Partial_Default/Replication_Package/Empirics"
*-------------------------------------------------------------


*************************************************************
* OPTIONS
*************************************************************
local first_year = 2006   // Select 1st year of the sample
local last_year  = 2011   // Select last year of the sample
*-------------------------------------------------------------


*************************************************************
*INFLATION MISREPORT
*************************************************************

use "00_data/01_datasets/00_Inputs/01_Arg_Inflation.dta", clear
keep Date month year  IPC_INDEC  IPC7

* Generate Misreport
* Note: Positive value implies positive underreport; Monthly inflation
	gen Inf_Misreport = -100*(IPC_INDEC - IPC7) / IPC_INDEC  
	
*Remove seasonality from the misreport
	sort Date
	gen n = _n
	reg Inf_Misreport i.month, nocons
	predict rr, res
	replace Inf_Misreport = rr

*Keep relevant dates
	keep if ( year>=`first_year' & year<=`last_year')
	drop if year==2006 & month==1  // No data for this month

*Export results
	gen mdate = mofd(Date)
	format mdate %tm
	keep year month Inf_Misreport
	order year month Inf_Misreport
	export excel using "02_SVAR/00_Data/Misrep_data.xls", firstrow(variables) replace
*------------------------------------------------------------------------------


*************************************************************
*EMBI SPREADS
*************************************************************
use "00_data/01_datasets/00_Inputs/02_Arg_merged.dta",clear
keep Date day month year EMBI_ARG VIX_CLOSE SP_CLOSE EEM_CLOSE

*Keep relevant dates
	keep if ( year>=`first_year' & year<=`last_year')
	drop if year==2006 & month==1
	sort Date
	gen n = _n

*Log variables
	gen EMBI_ln      = ln(EMBI_ARG)
	gen VIX_CLOSE_ln = ln(VIX_CLOSE)
	gen SP_CLOSE_ln  = ln(SP_CLOSE)
	gen EEM_CLOSE_ln = ln(EEM_CLOSE)

*Regress log EMBI on global controls & trend [daily regression]
	reg EMBI_ln VIX_CLOSE_ln SP_CLOSE_ln EEM_CLOSE_ln n
	predict rr ,res
	replace EMBI_ln = rr
	drop rr
	replace EMBI_ln = EMBI_ln *100
		
*Monthly Variables
	gen mdate = mofd(Date)
	format mdate  %tm
	sort Date
	tset n
	replace EMBI_ln = L.EMBI_ln if EMBI_ln[_n]==.
	collapse (median) EMBI_ln (mean) year month, by(mdate)  

*Export Results
	keep year month EMBI_ln
	order year month EMBI_ln
	export excel using "02_SVAR/00_Data/EMBI_data.xls", firstrow(variables) replace
*------------------------------------------------------------------------------


*************************************************************************
*EMAE (INDEX OF ECONOMIC ACTIVITY)
**************************************************************************
import excel "00_data/00_raw_data/01_Arg_EMAE.xls", firstrow clear
qui gen mdate = ym(year(date), month(date))
format mdate %tm
drop if mdate==.
sort mdate
gen n = _n
tsset mdate
qui gen year   = year(date)
qui gen month  = month(date)

*Log variables
qui gen lnEMAE     = ln(EMAE_SA)
qui gen lnUS_rate  = ln(Tresuary_rate)
qui gen lnUS_unmpl = ln(US_Unemployment)
qui gen lnUS_oil   = ln(Oil_price)		

*Regress log EMAE on global controls & trend [monthly regression; expand sample]
reg  lnEMAE lnUS_rate lnUS_unmpl  lnUS_oil  n
predict rr, res
replace lnEMAE = rr
replace lnEMAE = lnEMAE *100
		
keep year month lnEMAE
keep if ( year>=`first_year' & year<=`last_year')
sort year month
drop if year==2006 & month==1

*Export results
order year month lnEMAE
export excel using "02_SVAR/00_Data/IP_data.xls", firstrow(variables) replace


*************************************************************
* BREAK-EVEN INFLATION RATE
*************************************************************
use "00_data/01_datasets/00_inputs/03_BreakEvenInflation.dta", clear
keep Date BE Inf_Misreport EMBI_ARG 
qui gen year  = year(Date)
qui gen month = month(Date)

*Keep relevant dates
	keep if (year>=`first_year' & year<=`last_year')
	drop if year==2006 & month==1
	gen mdate    = mofd(Date)
	format mdate  %tm

*Define BE Change (shock)
	sort Date
	gen Date_id = _n
	tset Date_id
	gen BE_Change  = -1*(BE-L.BE)

* Generate Event Variable
	qui gen Event          = 0
	qui replace Event      = 1 if Inf_Misreport!=.
	qui gen in_window      = 0

* Create Event Window (3-days)
	qui replace in_window  = 1 if   Event==1  | L.Event==1  | F.Event==1 
	keep if in_window==1
	collapse (median) BE_Change (mean) year month, by(mdate)

*Export results
	keep  year month BE_Change
	order year month BE_Change
	export excel using "02_SVAR/00_Data/FACTOR_data.xls", firstrow(variables) replace
